package com.rupecm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;  
import javax.faces.context.FacesContext;  
import javax.servlet.ServletContext;  
  
import org.primefaces.model.DefaultStreamedContent;  
import org.primefaces.model.StreamedContent;  

import com.rupecm.artefato.Artefato;
import com.rupecm.artefato.ArtefatoRN;

public class FileDownloadController {

	private StreamedContent file;  
	private File arquivo;
	private ArtefatoRN artefatoRN = new ArtefatoRN(); 
    
    public FileDownloadController(Artefato artefatoSelecionado) throws FileNotFoundException {    
    	try {
			arquivo = artefatoRN.downloadAux(artefatoSelecionado);
		} catch (IOException e) {
			e.printStackTrace();
		}
        //InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/images/optimusprime.jpg");  
        InputStream stream = new FileInputStream(arquivo);
        file = new DefaultStreamedContent(stream, "application/vnd.ms-word", "TESTE_PRIME.doc");  
    }  
  
    public StreamedContent getFile() {  
        return file;  
    }    
}  